﻿using System;
using System.Collections.Generic;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.Helper;

namespace SdmxMl.Structure
{
    /// <summary>
    /// Partial target identifiers are subsets of the full target identifier. They are
    /// themselves given an identifier, so that they can be referenced by the metadata
    /// attributes of a report. A human-readable name must be provided, which may be provided
    /// in multiple, parallel-language versions. A longer, human-readable name may also be
    /// provided in multiple, language-parallel versions. Annotations may be provided.
    /// </summary>
    public class PartialTargetIdentifierType : BaseArtefact
    {
        #region Properties

        private string _urn;
        public virtual string Urn
        {
            get { return _urn; }
            set { _urn = value; }
        }

        private string _uri;
        public virtual string Uri
        {
            get { return _uri; }
            set { _uri = value; }
        }

        private List<string> _identifierComponentRef;
        public List<string> IdentifierComponentRef
        {
            get { return _identifierComponentRef; }
            set { _identifierComponentRef = value; }
        }

        
        #endregion // Properties

        #region Construction

        private void Init()
        {
            _identifierComponentRef = new List<string>();
        }

        public PartialTargetIdentifierType()
        {
            Init();

        }
        public PartialTargetIdentifierType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr) 
            : base(nodeArtefact, nsmgr)
        {     
            Init();
            _urn = Xmlhelper.GetAtt(nodeArtefact, "urn");
            _uri = Xmlhelper.GetAtt(nodeArtefact, "uri");

             string query ="./structure:IdentifierComponentRef";
            XmlNodeList nodeList = nodeArtefact.SelectNodes(query, nsmgr);
            if (nodeList.Count > 0)
            {
                foreach (XmlNode nd in nodeList)
                {
                    _identifierComponentRef.Add(nd.InnerText);
                }
            }
        }

        #endregion //Construction

        #region XML serialize

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);
            // id required
            if (string.IsNullOrEmpty(Id))
                Id = "undefined";
            xw.WriteAttributeString("id", Id);
            Xmlhelper.WriteAttributeIfExist(xw, "urn", Urn);
            Xmlhelper.WriteAttributeIfExist(xw, "uri", Uri);

            Name.WriteXml(xw, prefix, "Name");
            Description.WriteXml(xw, prefix, "Description");

            foreach (string it in _identifierComponentRef)
                xw.WriteElementString(prefix, "IdentifierComponentRef", null, it);


            // Annotations
            AnnotationList.WriteXml(xw, prefix, null);

            xw.WriteEndElement();
        }
        #endregion //XML serialize

    }

    public class PartialTargetIdentifierTypeList : List<PartialTargetIdentifierType>
    {
    }
}
